package com.alibaba.nacos.example.spring.cloud;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.example.spring.cloud.test.TccEntity;
import com.google.common.collect.Lists;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xiaojing
 */
@SpringBootApplication
@EnableDiscoveryClient
public class NacosProviderApplication {

	public static void main(String[] args) {
		SpringApplication.run(NacosProviderApplication.class, args);
	}

	@RestController
	class EchoController {
		@RequestMapping(value = "/echo/{string}", method = RequestMethod.GET)
		public String echo(@PathVariable String string) {
			return "Hello Nacos Discovery " + string;
		}

		@RequestMapping("test03")
		public List<TccEntity> get(){
			TccEntity tccEntity = new TccEntity();
			tccEntity.setSponsor("service-provider-tcc-test");
			tccEntity.setSponsorMethodName("test01/lock");
			tccEntity.setParamJsonStr("");
			List<String> list =  new ArrayList<String>();
			list.add("service-provider-tcc-test");
			list.add("service-consumer-tcc-test");
			tccEntity.setParpicipants(list);
			ArrayList<String> objects = Lists.newArrayList();
			objects.add("");
			objects.add("test02/lock");
			tccEntity.setParpicipantsMethodNames(objects);
			ArrayList<String> objects1 = Lists.newArrayList();
			Map<Object,Object> map  = new HashMap<Object, Object>();
			map.put("11","11");
			objects1.add(JSONObject.toJSONString(map));
			objects1.add(JSONObject.toJSONString(map));
			tccEntity.setParticipansParams(objects1);
			tccEntity.setTimestamp(System.currentTimeMillis());
			// confirm 阶段
			ArrayList<String> objects2 = Lists.newArrayList();
			objects2.add("test01/confirm");
			objects2.add("test02/confirm");
			tccEntity.setConfirmMethods(objects2);
			tccEntity.setConfirmParams(objects1);
			// cancel 阶段
			ArrayList<String> objects3 = Lists.newArrayList();
			objects3.add("test01/cancel");
			objects3.add("test02/cancel");
			tccEntity.setCancelMethodNames(objects3);
			tccEntity.setCancelParams(objects1);

			List<TccEntity> result =  new ArrayList<TccEntity>();

			result.add(tccEntity);
			result.add(tccEntity);

			return result;

		}

	}



}
